<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/yun.png" alt class="log-image" />
        <span>吕晓云盘</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '62px' : '200px'">
        <el-container>
          <el-main>
            <el-menu
                    :default-active="$route.path"
                    class="el-menu-vertical-demo"
                    @open="handleOpen"
                    @close="handleClose"
                    :collapse="isCollapse"
                    background-color="#333744"
                    text-color="#fff"
                    active-text-color="#ffd04b"
                    router
            >

              <el-submenu>
                <template slot="title" >
                  <div @click="isCollapse=!isCollapse">
                    <i :class="isCollapse ?'el-icon-d-arrow-right' :'el-icon-d-arrow-left'"/>
                    <span slot="title">{{isCollapse ? '打开' : '收起'}}</span>
                  </div>
                </template>
              </el-submenu>
              <el-submenu index="1">
                <template slot="title">
                  <i class="el-icon-coin"></i>
                  <span slot="title">我的云存储</span>
                </template>
                <el-menu-item index="/allfile"><i class="el-icon-folder"></i>
                  <span slot="title">全部文件</span></el-menu-item>
                <el-menu-item index="/photo">
                  <i class="el-icon-camera"></i>
                  <span slot="title">图片</span>
                </el-menu-item>
                <el-menu-item index="/video">
                  <i class="el-icon-video-play"></i>
                  <span slot="title">视频</span>
                </el-menu-item>
                <el-menu-item index="/document">
                  <i class="el-icon-document"></i>
                  <span slot="title">文档</span>
                </el-menu-item>
                <el-menu-item index="/music">
                  <i class="el-icon-headset"></i>
                  <span slot="title">音乐</span>
                </el-menu-item>
              </el-submenu>
              <el-menu-item index="/share">
                <i class="el-icon-menu"></i>
                <span slot="title">我的云分享</span>
              </el-menu-item>
              <el-menu-item index="/recover">
                <i class="el-icon-delete"></i>
                <span slot="title">我的回收站</span>
              </el-menu-item>
              <el-menu-item index="updateinfo">
                <i class="el-icon-setting"></i>
                <span slot="title">公告</span>
              </el-menu-item>
            </el-menu>
            <p><font color="#fff">{{'昵称：'+user.name}}</font></p>
            <p><font color="#fff">{{('容量：'+transSize(user.capacity))}}</font></p>
            <p><font color="#fff">{{('已用：'+transSize(user.size))}}</font></p>
            <el-progress :percentage="(user.size/user.capacity)*100" format="format"></el-progress>
          </el-main>
        </el-container>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import qs from "qs";

export default {
  data() {
    return {
      // 是否折叠
      isCollapse: true,
      user: ''
      // 被激活的链接地址
    };
  },
  created() {
    this.activePath = window.sessionStorage.getItem("activePath");
    this.$http.get("/api_user/cloud_user/get_by_id?id="+qs.parse(window.sessionStorage.getItem('jti')).id).then(res=>{
        this.user = res.data;
    })
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
    transSize(size){
      if(size == null) return
      if(size<1024){
        return size+'B'
      }else if(size<1048576){
        return (size/1024.0).toFixed(2)+'K'
      }else if(size<1073741824){
        return (size/1048576).toFixed(2)+'M'
      }else{
        return (size/1073741824).toFixed(2)+'G'
      }
    },
    format(percentage) {
      return percentage === 100 ? '满' : `${percentage}%`;
    }
  }
   
};
</script>

<style lang="less" scoped>
.el-container{
  height: 100%;
}
.el-radio-group {
  background-color: #333744;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
  .el-main{
    background-color: #333744;
  }
}

.el-main {
  background-color: #ffffff;
  padding: 0;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.log-image {
  width: 20%;
  height: 20%;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>

